begintownscript;

variables;
short choice;
short pc_counter;
short lead_char;

body;

beginstate 0; //INIT_STATE
	//initial setup
	if(get_flag(0,0) == 0) {
		pc_counter = 0;
		while(char_ok(pc_counter) == 0) {
			pc_counter = pc_counter + 1;
			}
		lead_char = pc_counter;
		relocate_character(0,17,18);
		relocate_character(1,18,17);
		relocate_character(2,19,18);
		relocate_character(3,18,19);
		force_view_center(char_loc_x(lead_char),char_loc_y(lead_char));
		force_instant_terrain_redraw();
		set_flag(0,0,1);
		toggle_quest(0,1);
		}

	set_name(6,"Timothy");
	set_name(12,"Elena");
	set_name(13,"Father Andrews");
	set_char_dialogue_pic(13,523,0);
	set_name(14,"Tyler"); //Does not spawn initially
	set_name(15,"Skip");
	set_name(16,"Duncan");
	set_name(17,"Commander Sigmund");
	set_name(18,"Mayor Skellick");
	set_char_dialogue_pic(18,524,0);
	set_name(19,"Secretary");
	set_name(20,"Liaison Torvald");
	set_char_dialogue_pic(20,522,0);
	set_name(21,"Child");
	set_name(22,"Child");
	set_name(23,"Child");
	add_range_to_group(24,41,1); //Soldiers
	add_char_to_group(7,1);
	add_char_to_group(8,1);
	add_char_to_group(11,1);
	set_name(1001,"Soldier");
	set_level(1001,35);

	set_crime_tolerance(2);
	enable_add_chars(1);

	if((get_flag(8,1) == 1) && (get_flag(0,0) < 2)) {
		reset_dialog();
		add_dialog_str(0,"As you triumphantly strut back into Stonesthrow, you can practically smell your reward.",0);
		if(has_item(449) == 1) {
			add_dialog_str(1,"Of course, at this point, you can also smell Trevahn's body. Predictably, it's pretty rank.",0);
			add_dialog_str(2,"At least you'll have proof that you killed him.",0);
			set_flag(0,22,1);
			}
		run_dialog(1);
		set_flag(0,0,2);
		}


break;

beginstate 1; //EXIT_STATE
break;

beginstate 2; //START_STATE
	//Checking for the lead pc each turn... not necessary here
	//pc_counter = 0;
	//while(char_ok(pc_counter) == 0) {
	//	pc_counter = pc_counter + 1;
	//	}
	//lead_char = pc_counter;
	
	//updating the braziers on the walls
	if((get_current_tick() % 5000 >= 3750) && ((get_flag(0,20) == 0) || (get_terrain(25,8) == 266))) {
		set_terrain(25,8,267);
		set_terrain(39,8,267);
		set_terrain(8,8,267);
		set_terrain(55,8,267);
		set_terrain(55,27,267);
		set_terrain(55,37,267);
		set_terrain(55,55,267);
		set_terrain(37,55,267);
		set_terrain(27,55,267);
		set_terrain(8,55,267);
		set_terrain(8,37,267);
		set_terrain(8,27,267);
		force_instant_terrain_redraw();
		set_flag(0,20,1);
		}
	if((get_current_tick() % 5000 < 3750) && ((get_flag(0,20) == 1) || (get_terrain(25,8) == 267))) {
		set_terrain(25,8,266);
		set_terrain(39,8,266);
		set_terrain(8,8,266);
		set_terrain(55,8,266);
		set_terrain(55,27,266);
		set_terrain(55,37,266);
		set_terrain(55,55,266);
		set_terrain(37,55,266);
		set_terrain(27,55,266);
		set_terrain(8,55,266);
		set_terrain(8,37,266);
		set_terrain(8,27,266);
		force_instant_terrain_redraw();
		set_flag(0,20,0);
		}
break;


beginstate 10; //leave the Smoking Halberd
	if(get_flag(0,4) > 0)
		end();
	reset_dialog();
	add_dialog_str(0,"You step out of the Smoking Halberd into the streets of Stonesthrow. It's fairly quiet.",0);
	add_dialog_str(1,"Looks like you should find your way to the mayor's office, so you can find out why you've been called out here.",0);
	run_dialog(1);
	set_flag(0,4,1);
break;

beginstate 15; //outhouse (door trigger)
	if(get_flag(0,6) == 1)
		end();

	force_instant_terrain_redraw();
	pause(4);
	
	message_dialog("As you open the door, a wave of foul stench washes over you.","Yep, this is definitely an outhouse.");
	set_flag(0,6,1);
break;

beginstate 20; //keep party from leaving early (also, end scenario)
	
	//ending the scenario
	if(get_flag(0,21) == 2) {
		block_entry(1);
		reset_dialog();
		add_dialog_str(0,"With your business here concluded, it is time to consider leaving Stonesthrow. Adventure certainly awaits you elsewhere, but are you ready to leave?",0);
		add_dialog_choice(0,"No, I'm not ready.");
		add_dialog_choice(1,"Yes, onward!");
		choice = run_dialog(1);
		if(choice == 2) {
			if(get_flag(0,24) == 1) {
				reset_dialog();
				add_dialog_str(0,"You quickly find a boat that will take you far, far away from this mess. Before you know it, you're somewhere equally tropical, but less troubled.",0);
				add_dialog_str(1,"Not long after you escape, you hear rumors that the Loyalists are trying to establish a foothold elsewhere. There are even stories about Trevahn.",0);
				add_dialog_str(2,"It seems that it'll only be a matter of time until you have to deal with this threat again...",0);
				add_dialog_str(3,"THE END",30);
				run_dialog(1);
				}
			end_scenario(1);
			}
		}
	
	
	//trying to leave before the resurrection scene
	if(get_flag(0,23) == 1) {
		reset_dialog();
		add_dialog_str(0,"You pause for a moment at the gates. It probably wouldn't be a good idea to leave the mayor waiting for you.",0);
		//if you have the body right now
		if(has_item(449) == 1)
			add_dialog_str(1,"Also, it would give you a simple way to get rid of this stinking corpse.",0);
		else
			add_dialog_str(1,"Of course, you should probably try to remember where you left the body first.",0);
		run_dialog(1);
		block_entry(1);
		end();
		}
	
	//leaving before having spoken with the mayor
	if(get_flag(0,16) < 3) {
		reset_dialog();
		add_dialog_str(0,"When you reach the gates of Stonesthrow, you pause. You still don't actually know why you were summoned here, and you were supposed to talk to the mayor and find out.",0);
		add_dialog_str(1,"You should probably do that before you leave town.",0);
		run_dialog(1);
		block_entry(1);
	}
	
	//leaving after speaking with the mayor
	else if(get_flag(0,18) == 0) {
		reset_dialog();
		add_dialog_str(0,"You stride out the gates of Stonesthrow, finally clear on your mission. All you have to do is fight off a mysterious group of rebels... lovely.",0);
		add_dialog_str(1,"Of course, before you can get to Copperpeak, you need a boat. If the mayor was right, you should be able to get one down at the docks.",0);
		run_dialog(1);
		set_flag(0,18,1);
	}
break;

beginstate 25; //statue (specobj trigger)
	reset_dialog();
	add_dialog_str(0,"This statue shows a man clutching a scroll and looking defiant. There's an inscription at the base:",0);
	add_dialog_str(1,"Lucio Marwell, 743-801. Founder of Stonesthrow, Explorer, Knight of the Empire.",10);
	run_dialog(1);
break;

beginstate 30; //gate wheels (specobj trigger)
	message_dialog("This is a large wooden wheel. A chain is wrapped around it several times and then disappears through a hole in the floor. However, there's a locking mechanism on the wheel, and a quick tug reveals that it is locked.","");
break;

beginstate 35; //Aaron's shop
	if(get_flag(0,7) == 0) {
		reset_dialog();
		add_dialog_str(0,"This shop seems to be abandoned, and it looks like it's been that way for quite some time.",0);
		run_dialog(1);
		set_flag(0,7,1);
		end();
		}
break;

beginstate 40; //chapel
	//barrier for the resurrection scene
	if(get_flag(0,23) == 1) {
		//if you don't have the body yet, stay out
		if(has_item(449) != 1) {
			message_dialog("You enter the chapel, and the mayor asks if you have Trevahn's body. When you tell him you don't, he frowns.","_Well, go get it! We don't have all day!_");
			block_entry(1);
			end();
			}
		//if you haven't actually been here before
		if(get_flag(0,8) != 1) {
			reset_dialog();
			add_dialog_str(0,"At a glance, it's apparent that this building is a chapel of sorts. It looks like it hasn't been used very much lately.",0);
			add_dialog_str(1,"One lone priest is pacing the room, explaining something to the mayor. Liasion Torvald is seated nearby, looking quite uncomfortable.",0);
			run_dialog(1);
			set_flag(0,8,1);
			}
		reset_dialog();
		add_dialog_str(0,"You enter the chapel, and the mayor immediately asks if you have Trevahn's body. When you answer in the affirmative, he smiles.",0);
		add_dialog_str(1,"_Excellent, let's get to work then!_",15);
		run_dialog(1);
		pause(3);
		
		//walk the party in
		march_party(16,37);
		force_view_center(16,37);
		force_instant_terrain_redraw();
		play_sound(-49);
		pause(3);
		march_party(16,38);
		force_view_center(16,37);
		force_instant_terrain_redraw();
		play_sound(-49);
		pause(3);
		march_party(16,39);
		force_view_center(16,37);
		force_instant_terrain_redraw();
		play_sound(-49);
		pause(3);
		if(party_size() > 3) {
			relocate_character(3,15,37);
			play_sound(-50);
			force_instant_terrain_redraw();
			pause(3);
			}
		if(party_size() > 4) {
			relocate_character(4,17,37);
			play_sound(-49);
			force_instant_terrain_redraw();
			pause(3);
			}
		pause(10);
		//dump out the body
		play_sound(82); //open backpack
		take_all_of_item(449); //just in case you had more than one
		put_item_on_spot(449,16,40);
		play_sound(101); //dropped item thud
		force_instant_terrain_redraw();
		reset_dialog();
		add_dialog_str(0,"You unceremoniously dump the body out on the ground, and the ceremony begins...",0);
		run_dialog(1);
		pause(3);
		reset_dialog();
		add_dialog_str(0,"When Trevahn is revived, he initially resists your interrogation. Luckily for you, the time that he spent dead weakened him enough for you to be able to restrain him.",0);
		add_dialog_str(1,"Over the next few hours, you learn a lot about the Loyalists' operations around Vantanas. Apparently, this was just a large-scale skirmish for their organization.",0);
		add_dialog_str(2,"When Mayor Skellick feels you have learned enough, guards are called to carry Trevahn away. You end up escorting him to Woodsmuir, where he is eventually executed.",0);
		add_dialog_str(3,"THE END",25);
		run_dialog(1);
		award_party_xp(500,10);
		end_scenario(1);
		}


	//first dialog for seeing the place
	if(get_flag(0,8) == 1)
		end();
	reset_dialog();
	add_dialog_str(0,"At a glance, it's apparent that this building is a chapel of sorts. It looks like it hasn't been used very much lately.",0);
	add_dialog_str(1,"One lone priest is pacing the room, occasionally stopping to touch the altar.",0);
	run_dialog(1);
	set_flag(0,8,1);
break;

beginstate 45; //forge (door trigger)
	if(get_flag(0,9) == 1)
		end();
	force_instant_terrain_redraw();
	pause(5);
	reset_dialog();
	add_dialog_str(0,"A blast of warm air greets you as you open the door.",0);
	run_dialog(1);
	set_flag(0,9,1);
break;

beginstate 50; //barracks
	if(get_flag(0,15) == 1)
		end();
	reset_dialog();
	add_dialog_str(0,"This appears to be the main barracks for Stonesthrow. A few off-duty soldiers cast a bored look at you as you enter.",0);
	run_dialog(1);
	set_flag(0,15,1);
break;

beginstate 51; //breaking into the armory
	change_crime_level(2);
break;

beginstate 60; //entering the mayor's office
	if(get_flag(0,16) >= 1)
		end();
	//let the party see the room before the dialog box pops up
	force_instant_terrain_redraw();
	pause(5);
	reset_dialog();
	add_dialog_str(0,"You open the door to the mayor's office, and the mayor immediately stands to greet you.",0);
	add_dialog_str(1,"_Ah, lovely. You're finally here. Come, we must speak._",15);
	run_dialog(1);
	pause(3);

	//figure out the first step into the room
	// requires finding lead pc
	pc_counter = 0;
	while(char_ok(pc_counter) == 0) {
		pc_counter = pc_counter + 1;
		}
	lead_char = pc_counter;
	if(char_loc_y(lead_char) <= 25) {
		march_party(38,24);
		force_view_center(38,24);
		}
	else {
		march_party(38,25);
		force_view_center(38,25);
		}
	//now, actually walk into the room
	play_sound(-49);
	force_instant_terrain_redraw();
	pause(3);
	march_party(39,24);
	force_view_center(39,24);
	play_sound(-50);
	force_instant_terrain_redraw();
	pause(3);
	march_party(40,24);
	force_view_center(40,24);
	play_sound(-49);
	force_instant_terrain_redraw();
	pause(3);
	march_party(41,24);
	force_view_center(41,24);
	play_sound(-50);
	force_instant_terrain_redraw();
	pause(5);
	block_entry(1);
	set_flag(0,16,1);
break;